package com.fu.shop.controller;

import com.fu.core.common.BusinessException;
import com.fu.core.common.ExceptionCode;
import com.fu.core.common.JsonResult;
import com.fu.core.pojo.Admin;
import com.fu.core.util.JwtUtil;
import com.fu.shop.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author 付龙江
 * @date 2022/1/28 15:47
 */
@RestController
@CrossOrigin
@RequestMapping("/login")
@Api(tags = "后台登录")
public class LoginController {


    AdminService adminService;
    @Autowired
    public LoginController(AdminService adminService) {
        this.adminService = adminService;
    }

    @PostMapping(value = "/adminLogin")
    @ApiOperation(value = "后台登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", required = true),
            @ApiImplicitParam(name = "pwd", value = "手机密码", dataType = "String", required = true)
    })
    public Map<String,Object> adminLogin(String phone, String pwd) throws Exception {
        //校验前端传值是否为空
        if (StringUtils.isEmpty(phone)||StringUtils.isEmpty(pwd)){
            System.out.println(phone+"------------"+pwd);
            throw new BusinessException(ExceptionCode.WRONG_PHONE_OR_PWD);
        }
        //通过电话和密码登录
        Admin admin = adminService.login(phone, pwd);
        //通过jwt生成token
        String token = JwtUtil.sign(admin.getAdminPhone(), admin.getAdminId(), admin.getShopId());
        JsonResult jsonResult = new JsonResult();
        jsonResult.setField("token",token);
        jsonResult.setCode("200");
        jsonResult.setField("msg","登陆成功");
        return jsonResult.getMap();
    }


    @PostMapping("/forgetPwd")
    @ApiOperation("忘记密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", required = true),
            @ApiImplicitParam(name = "newPwd", value = "新密码", dataType = "String", required = true),
            @ApiImplicitParam(name = "sendCode", value = "手机验证码", dataType = "String", required = true)
    })
    public Map<String, Object> forgetPwd(String phone,String sendCode,String newPwd) throws Exception {
        adminService.forgetPwd(phone, newPwd, sendCode);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        return jsonResult.getMap();
    }

    @PostMapping("/send")
    @ApiOperation("发送验证码接口")
    public Map<String, Object> sendSMS(String phone) {
        String s = adminService.sendLoginSms(phone);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("发送成功");
        jsonResult.setField("sendCode", s);
        return jsonResult.getMap();
    }
}
